The Arduino program is pretty straight forward. Plenty of comments are added to the code in order to make it understandable.
The main outline of the code is summarised below:
- Ask the user to set a force level by turning a potentiometer, then store the result when the OK button is pushed and set the servo to the desired position.
- Ask the user to set the number of squeezes by turning the same potentiometer.
- Tell the user to start squeezing.
- Check the displacement of the handle. This is done by checking the value read from the 6cm potentiometer (variable linPotPin).
This value is represented by a progress bar on the LCD, which is updated through the subroutine Progress.
- Count the number of squeezes. If the treshold linPotTreshMax is exceeded by the 6cm linear potentiometer,
the handle is squeezed sufficiently far and the squeezes counter is increased.
- Wait for the handle to return. This is done by a while loop, in which the position of the handle is checked until it reaches linPotTreshMin,
meaning that the handle is sufficiently released. This introduces some sort of hysteresis. Without it,
"cheating" would be possible by moving the handle just above and below its linPotTreshMax position.
- Check if the total number of squeezes matches the desired number of squeezes and if so, start the loop all over again.
As explained in 'Mechanics', the servo was modified to suit our needs.
Because of this modification, however, the standard Arduino servo library did no longer function correctly.
Some adjustments were needed in this library as well. Servoplus.cpp and Servoplus.h are the new files.
The are based on the Servo.cpp and Servo.h files found in your standard Arduino libraries folder (for instance C:\Program Files\arduino-0022\libraries\Servo).
Unfortunately,
simple information on how to adapt these libraries is hard to find and to understand without some knowledge about programming.
Because this is quite a simple operation, a two-step plan is given below:
Step 1
Open two new tabs in your Arduino sketchbook. Copy-paste the Servo.h and Servo.cpp files to these tabs and save them under a new name (for instance ServoPlus.cpp and ServoPlus.h)
Step 2
First of all, the name of the library is changed from Servo to Servoplus, or whatever name you chose. This means that, in the Servoplus.h file, the lines
#ifndef Servo_h
#define Servo_h
need to be changed into
#ifndef Servoplus_h
#define Servoplus_h
In the servo.cpp, the same has to be done with the line
#include "Servo.h"
Change it to
#include "Servoplus.h"
Step 3
The servo is controlled by pulses coming from a PWM (pulse width modulation).
The pulse width determines the position of the servo. This is what we want to adjust. We can do this by editing following lines.
#define MIN_PULSE_WIDTH 150 // the shortest pulse sent to a servo
#define MAX_PULSE_WIDTH 700 // the longest pulse sent to a servo
Set both values so that the desired range of your potentiometer is used. This will probably require some testing afterwards.
Step 4
Add servoplus.h to your main Arduino sketch. Pay attention! This is not done in the same way as you would include an original library. Instead of
#include
use
#include "Servoplus.h"
After adjusting MIN_PULSE_WIDTH and MAX_PULSE_WIDTH to the appropriate values, the servo should work exactly as you want it to.